package com.acc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.acc.dao.RoleUriDAO;
import com.acc.entity.AdminRole;
import com.acc.entity.RoleUri;
import com.acc.utils.JDBCTools;
/**
 * 
 * @author 朱春阳
 *
 */
public class RoleUriDAOImpl implements RoleUriDAO {

	@Override
	public List<RoleUri> getAllRoleUri(String sql, Object... objects) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<RoleUri> roleUris = new ArrayList<>();
		try {
			con = JDBCTools.getConnection();
			pst = con.prepareStatement(sql);
			int i = 1;
			for (Object object : objects) {

				pst.setObject(i, object);
				i++;
			}

			rs = pst.executeQuery();
			RoleUri roleUri = null;
			while (rs.next()) {
				roleUri = new RoleUri();
				roleUri.setRole_id(rs.getInt("role_id"));
				roleUri.setRole(rs.getString("role"));
				roleUri.setUri(rs.getString("uri"));
				roleUris.add(roleUri);
			}

			return roleUris;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pst, rs);
		}

		return roleUris;
	}

	@Override
	public Integer updateOneRoleUri(String sql, Object... objects) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCTools.getConnection();
			pst = con.prepareStatement(sql);
			int i = 1;
			for (Object object : objects) {

				pst.setObject(i, object);
				i++;
			}

			Integer num = pst.executeUpdate();
			return num;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pst, null);
		}
		return null;
	}

	@Override
	public List<RoleUri> getAllRU(Object... objects) {
		// TODO Auto-generated method stub
		String sql = "select * from ti_role where role_id=?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<RoleUri> roleUris = new ArrayList<>();
		try {
			con = JDBCTools.getConnection();
			pst = con.prepareStatement(sql);
			int i = 1;
			for (Object object : objects) {

				pst.setObject(i, object);
				i++;
			}

			rs = pst.executeQuery();
			RoleUri roleUri = null;
			while (rs.next()) {
				roleUri = new RoleUri();
				roleUri.setRole_id(rs.getInt("role_id"));
				roleUri.setRole(rs.getString("role"));
				roleUri.setUri(rs.getString("uri"));
				roleUris.add(roleUri);
			}

			return roleUris;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pst, rs);
		}

		return roleUris;
	}

	@Override
	public List<RoleUri> getAllRUByName(String roleName) {
		// TODO Auto-generated method stub
		String sql = "select * from ti_role where role=?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<RoleUri> roleUris = new ArrayList<>();
		try {
			con = JDBCTools.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, roleName);

			rs = pst.executeQuery();
			RoleUri roleUri = null;
			while (rs.next()) {
				roleUri = new RoleUri();
				roleUri.setRole_id(rs.getInt("role_id"));
				roleUri.setRole(rs.getString("role"));
				roleUri.setUri(rs.getString("uri"));
				roleUris.add(roleUri);
			}

			return roleUris;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pst, rs);
		}

		return roleUris;
	}

	/**
	 * 查询所有的ROLE-URI
	 */
	@Override
	public List<RoleUri> getAllPgae(Integer max, Integer min) {
		// TODO Auto-generated method stub
		String sql = "select * from ti_role";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<RoleUri> roleUris = new ArrayList<>();
		try {
			con = JDBCTools.getConnection();
			pst = con.prepareStatement(sql);
			
			rs = pst.executeQuery();
			RoleUri roleUri = null;
			while (rs.next()) {
				roleUri = new RoleUri();
				roleUri.setRole_id(rs.getInt("role_id"));
				roleUri.setRole(rs.getString("role"));
				roleUri.setUri(rs.getString("uri"));
				roleUris.add(roleUri);
			}

			return roleUris;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pst, rs);
		}

		return roleUris;
	}

}
